package com.fy.tt.service.impl;

import com.fy.tt.action.TemplateTool;
import com.fy.tt.dao.SemanticDao;
import com.fy.tt.entity.Semantic;
import com.fy.tt.entity.query.SemanticQuery;
import com.fy.tt.entity.vo.SemanticVo;
import com.fy.tt.service.SemanticService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

public class SemanticServiceImpl implements SemanticService {
    Connection dbConnection = null;
    SemanticDao semanticDao = null;
    {
        try {
            dbConnection = TemplateTool.getDBConnection();
            semanticDao = TemplateTool.getBean(SemanticDao.class);
        }catch (Exception e){
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }


    @Override
    public List<SemanticVo> getAllSemantics() {
        List<SemanticVo> semantics = null;
        try {
            semantics = semanticDao.getAllSemantics(dbConnection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return semantics;
    }

    @Override
    public Semantic getSemantic(int wordId) {
        Semantic semantic = null;
        try {
            semantic = semanticDao.getSemantic(dbConnection, wordId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return semantic;
    }

    @Override
    public int addSemantic(Semantic semantic) {
        int i = -1;
        try {
            int wordId = Integer.parseInt(semantic.getWordId().toString());
            semanticDao.updateIsTypeInByWordId(dbConnection,wordId,1);
            semanticDao.save(dbConnection, semantic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteSemantic(int wordId) {
        int i = -1;
        try {
            i = semanticDao.deleteSemantic(dbConnection, wordId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateSemantic(Semantic semantic) {
        int i = -1;
        try {
            i = (int) semanticDao.updateIgnoreNull(dbConnection, semantic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<SemanticVo> getLastSaveSemantic() {
        List<SemanticVo> allSemantics = this.getAllSemantics();
        List<SemanticVo> collect = allSemantics.stream().sorted((a, b) -> {
            return (int) (b.getUpdateTime().getTime() - a.getUpdateTime().getTime());
        }).limit(5L).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int getTotalSemantic(SemanticQuery semanticQuery) {
        int count = -1;
        try {
            count = semanticDao.getTotalSemantic(dbConnection, semanticQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int getTotalSemanticDeveloped(SemanticQuery semanticQuery) {
        int count = -1;
        try {
            count = semanticDao.getTotalSemanticDeveloped(dbConnection, semanticQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

}
